
#include "setting_download.h"
#include "sys_msg.h"
#include "msg_queue.h"
#include "root.h"
#include "pt_download.h"
#include "sys_font.h"
#include "pt_fs.h"

static ui_setting_dload_t  ui_set_dload;


static int setting_cancel_clicked_event(void)
{
    if(ui_set_dload.dload_timer){
        lv_timer_pause(ui_set_dload.dload_timer);
    }
    pt_download_set(0);

    page_pop_anim_name(LOAD_ANIM_MOVE_LEFT,"setting2");
    root_gesture_enable();

    return 0;
}

static void setting_cancel_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {

        debug_d("cancel clicked\n");
        setting_cancel_clicked_event();
    }
}


static int setting_download_status_update(void)
{
    int val = 0;
    char txt_buff[24];
    msg_notify_t msg_recv;

    val = pt_download_get();
    if(val >= 100)
    {
        pt_download_set(0);
        if(ui_set_dload.dload_timer){
            lv_timer_pause(ui_set_dload.dload_timer);
        }
        msg_recv.cmd   = MSG_DOWNLOAD_FIRMWARE;
        msg_recv.data0 = 0;
        msg_recv.data1 = 0;
        msg_queue_send(&msg_recv);
        return 0;
    }
    val ++;
    pt_download_set(val);

    memset(txt_buff,0,sizeof(txt_buff));
    snprintf(txt_buff,24,"%d%%",pt_download_get());

    lv_label_set_text( ui_set_dload.bar_val,txt_buff);
    lv_bar_set_value(ui_set_dload.bar_progress,pt_download_get(), LV_ANIM_ON);

    return 0;
}


void setting_download_timer_callback(struct _lv_timer_t * t)
{
    setting_download_status_update();
}

int ui_setting_download_create(lv_obj_t *superview)
{
    char txt_buff[24];

    ui_set_dload.dload_timer = NULL;

    //**********************************************************************************************
    ui_set_dload.title = lv_label_create(superview);
    lv_obj_set_pos(ui_set_dload.title,24,43);
    lv_obj_set_size(ui_set_dload.title,432,40);
    lv_obj_set_style_text_align(ui_set_dload.title,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_dload.title,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_dload.title,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_dload.title, lv_font_get(28), LV_STATE_DEFAULT);
    lv_label_set_text( ui_set_dload.title, "固件下载中");
    //**********************************************************************************************
    //download val

    ui_set_dload.bar_val = lv_label_create(superview);
    lv_obj_set_pos(ui_set_dload.bar_val,176,107);
    lv_obj_set_size(ui_set_dload.bar_val,128,40);
    lv_obj_set_style_text_align(ui_set_dload.bar_val,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_dload.bar_val,LV_OPA_40,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_dload.bar_val,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_dload.bar_val, lv_font_get(20), LV_STATE_DEFAULT);

    memset(txt_buff,0,sizeof(txt_buff));
    snprintf(txt_buff,24,"%d%%",pt_download_get());

    lv_label_set_text( ui_set_dload.bar_val,txt_buff);
    //**********************************************************************************************
    //download bar

	ui_set_dload.bar_progress = lv_bar_create(superview);

    lv_obj_set_style_radius(ui_set_dload.bar_progress, 5, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_dload.bar_progress, 0, LV_PART_MAIN);
    lv_obj_set_style_border_opa(ui_set_dload.bar_progress, LV_OPA_0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_dload.bar_progress, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);

	lv_obj_set_pos(ui_set_dload.bar_progress,62,151);
	lv_obj_set_size(ui_set_dload.bar_progress,356,12);
	lv_bar_set_range(ui_set_dload.bar_progress,0,100);
	lv_bar_set_mode(ui_set_dload.bar_progress, LV_BAR_MODE_SYMMETRICAL);

#if 0
	lv_obj_set_style_bg_opa(ui_set_dload.bar_progress, LV_OPA_0, LV_PART_MAIN);
#else
	lv_obj_set_style_bg_opa(ui_set_dload.bar_progress, LV_OPA_10, LV_PART_MAIN);
	lv_obj_set_style_bg_color(ui_set_dload.bar_progress, lv_color_make(0xFF, 0xFF, 0xFF), LV_PART_MAIN);
#endif

	//lv_obj_set_style_bg_opa(ui_set_dload.bar_progress, LV_OPA_100, LV_PART_INDICATOR);
	//lv_obj_set_style_bg_color(ui_set_dload.bar_progress, lv_color_make(0xc7, 0xc7, 0xcc), LV_PART_INDICATOR);

	lv_bar_set_value(ui_set_dload.bar_progress,pt_download_get(), LV_ANIM_ON);
    //**********************************************************************************************
    //tip_dload

    ui_set_dload.tip_dload = lv_label_create(superview);
    lv_obj_set_pos(ui_set_dload.tip_dload,24,196);
    lv_obj_set_size(ui_set_dload.tip_dload,432,40);
    lv_obj_set_style_text_align(ui_set_dload.tip_dload,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_dload.tip_dload,LV_OPA_40,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_dload.tip_dload,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_dload.tip_dload, lv_font_get(20), LV_STATE_DEFAULT);
    lv_label_set_text( ui_set_dload.tip_dload, "更新过程中设备会自动重启，请勿断电");
    //**********************************************************************************************
    //update btn

    ui_set_dload.bg_cancel = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_dload.bg_cancel,167,246);
    lv_obj_set_size(ui_set_dload.bg_cancel,170,60);

	lv_obj_set_style_radius(ui_set_dload.bg_cancel, 25, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_dload.bg_cancel, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_dload.bg_cancel, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_dload.bg_cancel, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_dload.bg_cancel, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_dload.bg_cancel, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_dload.bg_cancel, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_dload.bg_cancel, 0,0);
    
    #if 0
    lv_obj_set_style_bg_opa(ui_set_dload.bg_cancel,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_dload.bg_cancel,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_set_dload.bg_cancel,lv_color_make(0x26,0x7a,0xff),0);
    #endif 

    lv_obj_add_event_cb(ui_set_dload.bg_cancel, setting_cancel_event_callback, LV_EVENT_ALL, NULL);
    //**********************************************************************************************
    ui_set_dload.txt_cancel = lv_label_create(ui_set_dload.bg_cancel);
    lv_obj_set_pos(ui_set_dload.txt_cancel,0,10);
    lv_obj_set_size(ui_set_dload.txt_cancel,170,30);
    lv_obj_set_style_text_align(ui_set_dload.txt_cancel,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_dload.txt_cancel,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_dload.txt_cancel,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_dload.txt_cancel, lv_font_get(28), LV_STATE_DEFAULT);

    lv_label_set_text(ui_set_dload.txt_cancel,"取消");
    //**********************************************************************************************

    if(!ui_set_dload.dload_timer){
        ui_set_dload.dload_timer = lv_timer_create(setting_download_timer_callback,50,NULL);
    }else {
        lv_timer_reset(ui_set_dload.dload_timer);
    }
    //**********************************************************************************************

    return 0;
}

int ui_setting_download_destroy(lv_obj_t *superview)
{
   
    if(ui_set_dload.title){
        lv_obj_del(ui_set_dload.title);
        ui_set_dload.title = NULL;
    }
    if(ui_set_dload.bar_val){
        lv_obj_del(ui_set_dload.bar_val);
        ui_set_dload.bar_val = NULL;
    }
    if(ui_set_dload.bar_progress){
        lv_obj_del(ui_set_dload.bar_progress);
        ui_set_dload.bar_progress = NULL;
    }
    if(ui_set_dload.tip_dload){
        lv_obj_del(ui_set_dload.tip_dload);
        ui_set_dload.tip_dload = NULL;
    }
    if(ui_set_dload.bg_cancel){
        lv_obj_del(ui_set_dload.bg_cancel);
        ui_set_dload.bg_cancel = NULL;
        ui_set_dload.txt_cancel = NULL;
    }
    if(ui_set_dload.dload_timer){
        lv_timer_del(ui_set_dload.dload_timer);
        ui_set_dload.dload_timer = NULL;
    }

    return 0;
}



static void setting_download_on_view_did_load(page_base_t* self)
{
    ui_setting_download_create(self->root);
}

static void setting_download_on_view_did_unload(page_base_t* self)
{
    ui_setting_download_destroy(self->root);
}

static page_vtable_t setting_download_param = {
    .on_custom_attr_config  = NULL,
    .on_view_load           = NULL,
    .on_view_did_load       = setting_download_on_view_did_load,
    .on_view_will_appear    = NULL,
    .on_view_did_appear     = NULL,
    .on_view_will_disappear = NULL,
    .on_view_did_disappear  = NULL,
    .on_view_did_unload     = setting_download_on_view_did_unload,
};


page_vtable_t* lv_page_setting_download_create(void)
{
    return &setting_download_param;
}
